package com.JohnCarloSalter.ReceiptFactory;

import java.util.Calendar;
import java.util.ArrayList;

import com.JohnCarloSalter.Receipt.*;
import com.JohnCarloSalter.bestbuy_coupons.*;
import com.JohnCarloSalter.bestbuy_headers.*;
import com.JohnCarloSalter.bestbuy_rebates.*;

public class ReceiptFactory {
	private Receipt r;
	private SecondaryHeaderDecorator[] secondaryHeaders;
	private RebateDecorator[] rebates;
	private CouponDecorator[] coupons;
	
	public ReceiptFactory(ArrayList<PurchaseItem> listOfPurchases) {
		//Create basic receipt.
		r = new Receipt(new Purchases(listOfPurchases), Calendar.getInstance());
		
		//Create and add store header.	
		r.addStoreHeader(new StoreHeader(ConfigurationFile.getAddress(), 
				ConfigurationFile.getPhoneNumber(), 
				ConfigurationFile.getStoreNumber()));
		
		//Add sales tax class.
		r.addTaxMethod(getTax());
		
		//Initialize decorator list sizes.
		secondaryHeaders = ConfigurationFile.getSecondaryHeaderDecoratorList();
		rebates = ConfigurationFile.getRebateDecoratorList();
		coupons = ConfigurationFile.getCouponDecoratorList();	
	}
	
	public ReceiptComponent getReceipt() {
		ReceiptComponent userReceipt = r;
		
		//Initialize and link secondary headers.
		if(secondaryHeaders.length > 0) {
			for(int i = 0; i<secondaryHeaders.length; i++) {
				secondaryHeaders[i] = new SecondaryHeaderDecorator(r);
				userReceipt = secondaryHeaders[i];
			}
		}
		
		//Initialize and link the rebates.
		if(rebates.length > 0) {
			for(int i=0; i<rebates.length; i++) {
				rebates[i] = new RebateDecorator(userReceipt, r);
				if(rebates[i].applies()) {
					userReceipt = rebates[i];
				} 
			}
		}
		
		//Initialize and link the coupons.
		if(coupons.length > 0) {
			for(int i=0; i<coupons.length; i++) {
				coupons[i] = new CouponDecorator(userReceipt, r);
				if(coupons[i].applies()) {
					userReceipt = coupons[i];
				}
			}
		}
		
		return userReceipt;
	}
	
	
	//Utility Methods	
	private SalesTax getTax() {
		String stateCode = ConfigurationFile.getStateCode();
		switch (stateCode) {
			case "MD":
				return new MarylandTax();
			case "CA":
				return new CaliforniaTax();
			case "GA":
				return new GeorgiaTax();
			case "DE":
				return new DelawareTax();
			default:
				return null;
		}
	}
	
}

//Defunct Code
/*//Pass these methods the lengths of their relevant arrays.
//They will recursively link the different receipt components
//until the last item in the array. 
//At the last item in the array, these methods will return 
//the last unlinked receipt component.
//Note: THESE METHODS DO NOT CHECK IF THE DECORATORS APPLY.
//THIS MUST BE DONE LATER.
private ReceiptComponent getSecondaryHeaders(int i) {
	if(i == secondaryHeaders.length && i>1) {
		if(i == secondaryHeaders.length) {
			secondaryHeaders[i-1] = new SecondaryHeaderDecorator();
			secondaryHeaders[i-2] = new SecondaryHeaderDecorator(initializeSecondaryHeaders(i-1));
			return secondaryHeaders[i-1];
		} else if(i > 1) {
			secondaryHeaders[i-1] = new SecondaryHeaderDecorator(initializeSecondaryHeaders(i));
			return secondaryHeaders[i-1];
		} else { //i == 1
			secondaryHeaders[i-1] = new SecondaryHeaderDecorator(secondaryHeaders[i]);
			return secondaryHeaders[i-1];
		}
	} else if (i == secondaryHeaders.length && i == 1) {
		secondaryHeaders[i-1] = new SecondaryHeaderDecorator();
		return secondaryHeaders[i-1];
	} else {
		System.err.println("Illegal parameter passed to initializeSecondaryHeaders method.");
		System.err.println("It is also possible that no SecondaryHeaders exist.");
		return null;
	}
}

/*private ReceiptComponent initializeRebates(int i) {
	if(i == rebates.length && i>1) {
		if(i == rebates.length) {
			rebates[i-1] = new RebateDecorator(r);
			rebates[i-2] = new RebateDecorator(initializeRebates(i-1), r);
			return rebates[i-1];
		} else if(i > 1) {
			rebates[i-1] = new RebateDecorator(initializeRebates(i), r);
			return rebates[i-1];
		} else { //i == 1
			rebates[i-1] = new RebateDecorator(rebates[i], r);
			return rebates[i-1];
		}
	} else if (i == rebates.length && i == 1) {
		rebates[i-1] = new RebateDecorator(r);
		return rebates[i-1];
	} else {
		System.err.println("Illegal parameter passed to initializeRebates method.");
		System.err.println("It is also possible that no rebates exist.");
		return null;
	}
}

private ReceiptComponent initializeCoupons(int i) {
	if(i == coupons.length && i>1) {
		if(i == coupons.length) {
			coupons[i-1] = new CouponDecorator(r);
			coupons[i-2] = new CouponDecorator(initializeCoupons(i-1), r);
			return coupons[i-1];
		} else if(i > 1) {
			coupons[i-1] = new CouponDecorator(initializeCoupons(i), r);
			return coupons[i-1];
		} else { //i == 1
			coupons[i-1] = new CouponDecorator(coupons[i], r);
			return coupons[i-1];
		}
	} else if (i == coupons.length && i == 1) {
		coupons[i-1] = new CouponDecorator(r);
		return coupons[i-1];
	} else {
		System.err.println("Illegal parameter passed to initializeCoupons method.");
		System.err.println("It is also possible that no coupons exist.");
		return null;
	}
}*/
